/*
 * @Author: your name
 * @Date: 2021-07-07 15:01:29
 * @LastEditTime: 2022-05-26 16:46:33
 * @LastEditors: 11 11
 * @Description: In User Settings Edit
 * @FilePath: \exc-tl1-2\SRC\uart.h
 */
#ifndef UART_H
#define UART_H

#include "include.h"
extern u8 logEN, testEN;

typedef enum
{
	GPIO_MODE_OUT = 0,
	GPIO_MODE_IN = 1
} eGPIOMode_TypeDef;

typedef enum
{
	USART_EVEN_PARITY = 0x08, //Ð£ÑéÎ»
	USART_ODD_PARITY = 0x0C,
	USART_NO_PARITY = 0x00,

	USART_1STOP_SBIT = 0x00, //Í£Ö¹Î»
	USART_2STOP_SBIT = 0x10,

	USART_5BIT_DAT = 0x00, //Êý¾ÝÎ»
	USART_6BIT_DAT = 0x01,
	USART_7BIT_DAT = 0x02,
	USART_8BIT_DAT = 0x03,

	USART_BPS_4800 = 0x00,
	USART_BPS_7200 = 0x20,
	USART_BPS_9600 = 0x40,
	USART_BPS_19200 = 0x60,
	USART_BPS_115200 = 0x80
} eUsartPara_TypeDef;

#define PinMode_RX(x) ((x == GPIO_MODE_OUT) ? (GPIO->PMA &= 0xfffeffff) : (GPIO->PMA |= 0x00010000)) // P2.0
#define PinMode_TX(x) ((x == GPIO_MODE_OUT) ? (GPIO->PMA &= 0xfffdffff) : (GPIO->PMA |= 0x00020000)) // P2.1

/*这段内存一定不可以改变，如果需要增加变量，就增加在最后面*/
#pragma pack(1)
typedef struct
{
	u8 *tx_data;
	u8 send_cplt;
	u16 tx_len;
	u16 rx_len;
	u8 timeOut;
	u8 rx_ok;
	u8 tx_ok;
} uartStruct;
#pragma pack()
void uart_init(u8 Cfg);
void uart0_send(u8 *tx_buf, u16 len);
void uart1_send(u8 *tx_buf, u16 len);
void uart0_rx_dataReset(void);
void uart1_rx_dataReset(void);
void com_rx_task(void);
void uart2_send(u8 *tx_buf, u16 len);
void uart2_rx_dataReset(void);
extern uartStruct m_uart0, m_uart1, m_uart2;
#define DELAY                                                                  \
	systick.time_out = 0;                                                      \
	while ((m_uart0.send_cplt || m_uart0.send_cplt) && systick.time_out < 500) \
	{                                                                          \
		WDT->EN = 0xbb;                                                        \
	}
#define DELAY_UART1                                                            \
	systick.time_out = 0;                                                      \
	while ((m_uart1.send_cplt || m_uart1.send_cplt) && systick.time_out < 500) \
	{                                                                          \
		WDT->EN = 0xbb;                                                        \
	}
	
#define DELAY_UART2                                                            \
systick.time_out = 0;                                                      \
while ((m_uart2.send_cplt || m_uart2.send_cplt) && systick.time_out < 500) \
{                                                                          \
	WDT->EN = 0xbb;                                                        \
}
#define LOGD(...)                                                                                   \
	if (logEN)                                                                                      \
	{                                                                                               \
		memset(pcUart.pc_tx_buf, 0, sizeof(pcUart.pc_tx_buf));                                        \
		uart0_send(pcUart.pc_tx_buf, sprintf((char *)pcUart.pc_tx_buf, (const char *)__VA_ARGS__)); \
		DELAY                                                                                       \
	}
#define LOGD_TEST(...)                                                                              \
	if (testEN)                                                                                     \
	{                                                                                               \
		memset(pcUart.pc_tx_buf, 0, sizeof(pcUart.pc_tx_buf));                                        \
		uart0_send(pcUart.pc_tx_buf, sprintf((char *)pcUart.pc_tx_buf, (const char *)__VA_ARGS__)); \
		DELAY                                                                                       \
	}
#define LOGD_PC(...)                                                                                \
	if (1)                                                                                          \
	{                                                                                               \
		memset(pcUart.pc_tx_buf, 0, sizeof(pcUart.pc_tx_buf));                                        \
		uart0_send(pcUart.pc_tx_buf, sprintf((char *)pcUart.pc_tx_buf, (const char *)__VA_ARGS__)); \
		DELAY                                                                                       \
	}
#if (PROJECT_TYPEDEF == NB || PROJECT_TYPEDEF == NB_ONENET)
void uart_switch_baud(u8 Cfg);
#endif
#endif
